<?php
declare (strict_types=1);

namespace app\index\controller;

use app\admin\model\PostClickShare;
use app\admin\model\PostPath;
use app\admin\model\ResourceCollect;
use app\admin\model\ResourcePath;
use app\admin\model\TodayHot;
use app\AliOosFile;
use app\index\model\PostCate;
use app\index\model\Post as PostModel;
use app\index\model\PostComment;
use app\index\model\PostCommentLike;
use app\index\model\PostLike;
use app\index\model\SensitiveWord;
use app\index\model\UsersModel;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;
use app\index\validate\Post as PostValidate;
use app\admin\model\Resource as iResource;

require_once '../extend/emoji/emoji.php';

/**
 * 帖子管理
 * Class Post
 * @package app\index\controller
 */
class Post extends Base
{
    /**
     * 帖子分类
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function postCate()
    {
//        $userInfo = $this->checkLogin();
//        Log::write('用户:' . $userInfo['nickname'] . '获取了帖子分类', 'error');

        $data = PostCate::field('id,name')->select();
        $data = $data ? $data->toArray() : [];
        $this->responseJson(10200, 'success', $data);
    }

    /**
     * 今日热议
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function todayHot()
    {
        $size = intval($this->request->param('size')) ?: 20;
        $show_size = intval($this->request->param('show_size')) ?: 5;
        $data = TodayHot::limit($size)
            ->order('hot', 'desc')
            ->where('status', 1)
            ->field('id,title,hot')
            ->select();
        $data = $data ? $data->toArray() : [];
        if (!$data) {
            $this->responseJson(10400, 'empty');
        }

        foreach ($data as $k => $v) {
            $data[$k]['hot'] = format_num($v['hot']);
        }

        $data = array_chunk($data, $show_size);


        $this->responseJson(10200, 'success', $data);
    }

    /**
     * 今日热议详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function hotDetail()
    {
        if (!$this->request->isPost()) {
            $this->responseJson(10500, '发生异常');
        }

        $id = $this->request->param('id');
        if (!$id) {
            $this->responseJson(10500, '参数错误');
        }

        $find = TodayHot::where('id', $id)->hidden(['status', 'update_time'])->find();
        if (empty($find)) {
            $this->responseJson(10400, 'empty');
        }

        // 正则追加url
        $find['content'] = preg_replace('/(<img.+?src=")(.*?)/', '$1' . config('url.my_url') . '$2', $find['content']);
        $find['content'] = preg_replace('/(<video.+?src=")(.*?)/', '$1' . config('url.my_url') . '$2', $find['content']);

        $this->responseJson(10200, 'success', $find);

    }

    /**
     * 选项卡-分类帖子数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function postOptions()
    {
        // 默认查询全部帖子
        // 2=>开放区   3=>企业区   4=>商家区   5=>兴趣区   6=>云链专栏
        $pcid = intval($this->request->param('pcid')) ?: 0;
        $page = intval($this->request->param('page')) ?: 1;
        $pageSize = intval($this->request->param('pagesize')) ?: 10;

        // 1正常   4已通过（前端提交的帖子）
        $status = '1,4';
        $whereOr = [];
        $type = $this->request->header('type');
        switch ($type) {
            case 0:
                $token = $this->request->header('token');
                $loginType = '小程序';
                $where = ['token' => $token];
                break;
            case 1:
                $token = $this->request->header('token1');
                $loginType = '公众号';
                $where = ['token1' => $token];
                break;
            default:
                $token = '';
                $loginType = '未知';
                $where = [];
        }

        $isLogin = 0;
        // 查询账号信息
        $users = UsersModel::getUserInfo($where);
        if (!empty($users) && $users['state'] == 1) {
            Log::write('' . $loginType . '用户' . $users['nickname'] . '查询待审核帖子:' . $token, 'error');
            $isLogin = 1;
            // 用户已登录，根据uid查询是否存在待审核帖子
            $loginWhere = ['uid' => $users['id'], 'status' => 0];
            if ($pcid > 0) {
                $loginWhere['pcid'] = $pcid;
            }
            $idArr = PostModel::where($loginWhere)->column('id');
//                var_dump($idArr);
        }

        if (isset($idArr) && !empty($idArr)) {
            $idStr = rtrim(join(',', $idArr), ',');
            $whereOr[] = ['p.id', 'in', $idStr];
        }

        $wherePcid = [];
        if ($pcid > 0) {
            $wherePcid = ['p.pcid' => $pcid];
        }

        $info = PostModel::alias('p')
            ->field('p.id,p.rid,p.status,p.uid,p.pcid,p.content,p.location,p.create_time,p.lng,p.lat')
            ->where($wherePcid)
            ->whereIn('p.status', $status)
            ->whereOr($whereOr)
            ->order('p.create_time', 'desc')
            ->page($page, $pageSize)
            ->select();

        $info = $info ? $info->toArray() : [];
        if (!$info) {
            $this->responseJson(10400, 'empty', ['data' => [], 'count' => 0]);
        }

        $count = PostModel::alias('p')->where($wherePcid)->whereIn('p.status', $status)->whereOr($whereOr)->count();

        foreach ($info as $k => $v) {
            // 获取社群数据
            if ($v['rid']) {
                $resource = iResource::alias('r')
                    ->join('admin_location p', 'r.province = p.location_id', 'left')
                    ->join('admin_location c', 'r.city = c.location_id', 'left')
                    ->join('admin_users u', 'r.uid = u.id', 'left')
                    ->where(['r.id' => $v['rid']])
                    ->field('r.id,r.name,r.qrcode,r.desc,r.content,u.headimg,p.name pname,c.name cname')
                    ->find();
                if (!empty($resource)) {
                    $resource['qrcode'] = config('url.url') . $resource['qrcode'];
                    if ($resource['content']) {
                        $content = cutstr_html($resource['content']);
                        $content = mb_substr($content, 0, 150, 'UTF-8');
                        $resource['content'] = $content;
                    }
                    $resource['collect_yourself'] = 0;
                    if ($isLogin) {
                        $isCollect = ResourceCollect::where(['uid' => $users['id'], 'rid' => $v['rid']])->find();
                        // 收藏了
                        if (!empty($isCollect)) {
                            $resource['collect_yourself'] = 1;
                        }
                    }

                    $resource['location'] = str_replace(['省', '市'], '', $resource['pname'] . '·' . $resource['cname']);
                    unset($resource['pname'], $resource['cname']);

                    $images = ResourcePath::where(['rid' => $resource['id'], 'type' => 1])->field('id,path')->select();
                    $images = $images ? $images->toArray() : [];
                    foreach ($images as $k1 => $v1) {
                        $images[$k1]['path'] = config('url.url') . $v1['path'];
                    }
                    $resource['images'] = $images;
                }
            } else {
                $resource = [];
            }
            $info[$k]['resource'] = $resource;
            $users = UsersModel::where('id', $v['uid'])->field('nickname,headimg')->find();
            $info[$k]['headimg'] = $users['headimg'];
            $info[$k]['nickname'] = emojiDecode($users['nickname']);
            $info[$k]['cate_name'] = PostCate::where('id', $v['pcid'])->value('name');
            $path = PostPath::where('post_id', $v['id'])->hidden(['create_time'])->select();
            $path = $path ? $path->toArray() : [];
            $imagesPath = [];
            $videoPath = [];
            foreach ($path as $k1 => &$v1) {
//                var_dump($v1['path']);
                if ($v1['path']) {
                    $v1['path'] = config('url.url') . $v1['path'];
                    if ($v1['type'] == 2) {
                        $videoPath[] = $v1;
                    } else {
                        $imagesPath[] = $v1;
                    }
                }
            }

            if (count($imagesPath) <= 1) {
                foreach ($imagesPath as $k2 => $v2) {
                    $tmp = [$v2['width'], $v2['height']];
                    $max = max($tmp);
                    $min = min($tmp);
                    $lastSize = $max - $min;
                    $imagesPath[$k2]['dis_method'] = 'aspectFill';
                    $imagesPath[$k2]['heights'] = '300px';
                    if ($lastSize >= 200) {
                        $keys = array_search($max, $tmp);
                        // 1高度  2宽度
                        $imagesPath[$k2]['dis_method'] = $keys == 1 ? 'heightFix' : 'widthFix';
                        $imagesPath[$k2]['heights'] = $keys == 1 ? '250px' : 'auto';
                    }
                }
            }
            $info[$k]['images'] = $imagesPath;
            $info[$k]['video'] = $videoPath;
            $info[$k]['times'] = friendlyDate(strtotime($v['create_time']));
            // 登录了，标识是否点赞了帖子
            $info[$k]['like_yourself'] = 0;
            if ($isLogin == 1) {
                $info[$k]['like_yourself'] = PostLike::where(['post_id' => $v['id'], 'uid' => $users['id']])->count();
            }
            $info[$k]['like'] = format_num(PostLike::where('post_id', $v['id'])->count());
            $info[$k]['share'] = format_num(intval(PostClickShare::where(['post_id' => $v['id'], 'type' => 2])->sum('nums')));
            $info[$k]['comment'] = PostComment::where('post_id', $v['id'])->count();
        }

        $this->responseJson(10200, 'success', ['data' => $info, 'count' => $count]);
    }

    /**
     * 帖子添加
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addPost()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '正在添加帖子', 'error');

        if (!$this->request->isPost()) {
            $this->responseJson(10500, '发生异常');
        }

        $param = $this->request->param();
        if (!isset($param['uid'])) {
            $this->responseJson(10500, '参数错误');
        }

        if ($param['uid'] != $userInfo['id']) {
            $this->responseJson(10500, '不允许的操作');
        }

        // 必须上传其一
        if (empty($param['images']) && empty($param['content']) && empty($param['video'])) {
            $this->responseJson(10500, '随便发布点东西吧');
        }


        if (isset($param['content']) && !empty($param['content'])) {
            // 本地测试不请求微信检测
            if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
                $wechat = new Wechat();
                $wxToken = $wechat->GetToken1();
                if (!$wxToken) {
                    Log::write('addPost_token:' . json_encode($wxToken), 'error');
                    $this->responseJson(10500, '验证标识出错');
                }

                $postData = json_encode(['content' => $param['content']], JSON_UNESCAPED_UNICODE);
                // 内容安全接口 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html
                // 频率限制：单个 appId 调用上限为 4000 次/分钟，2,000,000 次/天
                $url1 = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=%s";
                $checkUrl = sprintf($url1, $wxToken);
                Log::write('帖子添加-我是请求微信的token:' . $wxToken, 'error');

                $contentRes = httpCurl($checkUrl, $postData, 'POST');
                $contentRes = json_decode($contentRes, true);
                Log::write('内容安全结果:' . json_encode($contentRes), 'error');
                // 87014 => 内容含有违法违规内容
                if ($contentRes['errcode'] == 87014) {
                    // 存储违规词
                    $finds = SensitiveWord::where('badword', $param['content'])->find();
                    if (empty($finds)) {
                        SensitiveWord::create([
                            'badword' => $param['content'],
                        ]);
                    }
                    Log::write('帖子添加-wx_content_errmsg：' . json_encode($contentRes), 'error');
                    $this->responseJson(10500, '存在违规词汇1');
                }
            }
        }

        try {
            validate(PostValidate::class)->sceneInsert()->check($param);
        } catch (ValidateException $e) {
            $this->responseJson(10500, $e->getMessage());
        }

        // 启动事务
        Db::startTrans();
        try {

            $res = PostModel::create($param);
            if (!$res) {
                $this->responseJson(10500, '添加失败');
            }
            $imageArr = [];
            // 上传了图片
            if (isset($param['images']) && !empty($param['images'])) {
                $imageArr = explode(',', $param['images']);
            }
            // 上传了视频
            if (isset($param['video']) && !empty($param['video'])) {
                $imageArr['video'] = $param['video'];
            }

            if (!empty($imageArr)) {
                // 存储图片/视频路径到指定表
                foreach ($imageArr as $type => $path) {
                    // 存储值：图片 => 1   视频 => 2
                    $type = $type === 'video' ? 2 : 1;
                    $width = 0;
                    $height = 0;
                    if ($type == 1) {
                        Log::write('正在获取' . $path . '的图片', 'error');
                        $imagesInfo = getimagesize($_SERVER['DOCUMENT_ROOT'] . $path);
                        if (!is_array($imagesInfo)) {
                            Log::write('获取' . json_encode($path) . '图片信息错误' . json_encode($imagesInfo), 'error');
                        }
                        $width = $imagesInfo[0];
                        $height = $imagesInfo[1];
                    }
                    PostPath::create([
                        'path' => $path,
                        'type' => $type,
                        'post_id' => $res->id,
                        'width' => $width,
                        'height' => $height,
                    ]);
                }

                // 存储图片/视频到阿里云
                foreach ($imageArr as $url) {
                    $AliOos = new AliOosFile();
                    // 去除开头的 /
                    $urls = substr($url, 1);
                    $AliOos->upload($_SERVER['DOCUMENT_ROOT'] . $url, $urls);
                }
            }

            // 提交事务
            Db::commit();

        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $error[1] = json_encode($param);
            $error[2] = $e->getMessage();
            Log::write($error, 'error');
            $this->responseJson(10500, $e->getMessage());
        }

        $this->responseJson(10200, 'success');
    }

    /**
     * 帖子点击量
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function postClick()
    {
        if (!$this->request->isPost()) {
            $this->responseJson(10500, '发生异常');
        }

        $param = $this->request->param();
        if (!isset($param['id']) || !isset($param['type'])) {
            $this->responseJson(10500, '参数错误');
        }

        $where = ['post_id' => $param['id'], 'type' => $param['type']];
        if (isset($param['uid'])) {
            $where['uid'] = $param['uid'];
        }

        $find = PostClickShare::where($where)->find();

        if (empty($find)) {
            $res = PostClickShare::create($where);
        } else {
            $res = PostClickShare::where($where)->inc('nums')->update();
        }

        if (!$res) {
            $this->responseJson(10500, 'error');
        }

        $this->responseJson(10200, 'success');
    }

    /**
     * 帖子分享量
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function postShare()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '帖子分享量统计', 'error');

        if (!$this->request->isPost()) {
            $this->responseJson(10500, '发生异常');
        }

        $param = $this->request->param();
        if (!isset($param['uid']) || !isset($param['id']) || !isset($param['type'])) {
            $this->responseJson(10500, '参数错误');
        }

        if ($param['uid'] != $userInfo['id']) {
//            $this->responseJson(10500, '不允许的操作');
        }

        $isExists = PostModel::find($param['id']);
        if (empty($isExists)) {
            $this->responseJson(10400, 'empty');
        }

        // 待审核帖子，不新增分享量
        if ($isExists['status'] == 0 && $param['type'] == 2) {
            $this->responseJson(10200, '审核中');
        }

        $where = ['uid' => $param['uid'], 'post_id' => $param['id'], 'type' => $param['type']];
        // 分享方式 1微信好友  2微信朋友圈
        if (isset($param['share_type'])) {
            $where['share_type'] = $param['share_type'];
        }

        $find = PostClickShare::where($where)->find();

        if (empty($find)) {
            $res = PostClickShare::create($where);
        } else {
            $res = PostClickShare::where($where)->inc('nums')->update();
        }

        if (!$res) {
            $this->responseJson(10500, 'error');
        }

        $this->responseJson(10200, 'success');
    }


    /**
     * 帖子点赞操作
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function postLike()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '帖子点赞操作', 'error');

        if (!$this->request->isPost()) {
            $this->responseJson(10500, '发生异常');
        }

        $param = $this->request->param();
        if (!isset($param['uid']) || !isset($param['id']) || !isset($param['operate'])) {
            $this->responseJson(10500, '参数错误');
        }

        if ($param['uid'] != $userInfo['id']) {
            $this->responseJson(10500, '不允许的操作');
        }

        $isExists = PostModel::find($param['id']);
        if (empty($isExists)) {
            $this->responseJson(10400, 'empty');
        }

        $where = ['uid' => $param['uid'], 'post_id' => $param['id']];
        // 是否已点赞
        $find = PostLike::where($where)->find();
        // 点赞
        if ($param['operate'] >= 1) {
            // 不存在，写入
            if (empty($find)) {
                $res = PostLike::create($where);
            }
            // 取消点赞
        } else {
            // 存在，删除
            if (!empty($find)) {
                $res = PostLike::where($where)->delete();
            }
        }

        $this->responseJson(10200, 'success', ['id' => $param['id'], 'nums' => $param['operate']]);
    }

    /**
     * 我的帖子
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function myPost()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '查看我的帖子操作', 'error');

        $param = $this->request->param();
        if (!isset($param['uid'])) {
            $this->responseJson(10500, '参数错误');
        }

        if ($param['uid'] != $userInfo['id']) {
//            $this->responseJson(10500, '不允许的操作');
        }

        $page = intval($this->request->param('page')) ?: 1;
        $pageSize = intval($this->request->param('pagesize')) ?: 10;

        // status 0待审核 1正常 4已通过
        $data = PostModel::alias('p')
            ->join('admin_post_cate c', 'p.pcid = c.id', 'left')
            ->join('admin_users u', 'p.uid = u.id', 'left')
            ->where('p.uid', $param['uid'])
            ->whereIn('p.status', '0,1,4')
            ->order('p.create_time', 'desc')
            ->page($page, $pageSize)
            ->field('p.id,p.uid,p.pcid,p.content,p.location,p.create_time,c.name,u.nickname,u.headimg,p.lng,p.lat')
            ->select();


        $data = $data ? $data->toArray() : [];

        if (!$data) {
            $this->responseJson(10400, 'empty', ['data' => [], 'count' => 0]);
        }

        $count = PostModel::alias('p')->where('p.uid', $param['uid'])->whereIn('p.status', '0,1,4')->count();

        foreach ($data as $k => $v) {
            $path = PostPath::where('post_id', $v['id'])->hidden(['create_time'])->select();
            $path = $path ? $path->toArray() : [];
            $imagesPath = [];
            $videoPath = [];
            foreach ($path as $k1 => &$v1) {
//                var_dump($v1['path']);
                if ($v1['path']) {
                    $v1['path'] = config('url.url') . $v1['path'];
                    if ($v1['type'] == 2) {
                        $videoPath[] = $v1;
                    } else {
                        $imagesPath[] = $v1;
                    }
                }
            }

            $data[$k]['images'] = $imagesPath;
            $data[$k]['video'] = $videoPath;
            $data[$k]['create_time'] = friendlyDate(strtotime($v['create_time']));
//            $data[$k]['create_time'] = date('n'."月".'d'."日".' H:i',strtotime($v['create_time']));
            // 自己是否点赞了
            $data[$k]['like_yourself'] = PostLike::where(['post_id' => $v['id'], 'uid' => $v['uid']])->count();
            $data[$k]['like'] = format_num(PostLike::where(['post_id' => $v['id']])->count());
            $data[$k]['share'] = format_num(intval(PostClickShare::where(['post_id' => $v['id'], 'type' => 2])->sum('nums')));
            $data[$k]['comment'] = PostComment::where('post_id', $v['id'])->count();
        }

        $this->responseJson(10200, 'success', ['data' => $data, 'count' => $count]);
    }

    /**
     * 帖子详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function postDetail()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '查看我的帖子操作', 'error');

        $param = $this->request->param();
        if (!isset($param['id'])) {
            $this->responseJson(10500, '参数错误');
        }

        $info = PostModel::alias('p')
            ->join('admin_users u', 'p.uid = u.id', 'left')
            ->join('admin_post_cate c', 'p.pcid = c.id', 'left')
            ->field('p.id,p.status,p.uid,p.pcid,p.content,p.location,p.create_time,u.nickname,u.headimg,c.name cate_name')
            ->where('p.id', $param['id'])
            ->find();

        $info = $info ? $info->toArray() : [];

        if (empty($info) || !in_array($info['status'], [0, 1, 4])) {
            $this->responseJson(10400, 'empty');
        }

        $type = $this->request->header('type');
        switch ($type) {
            case 0:
                $token = $this->request->header('token');
                $loginType = '小程序';
                $where = ['token' => $token];
                break;
            case 1:
                $token = $this->request->header('token1');
                $loginType = '公众号';
                $where = ['token1' => $token];
                break;
            default:
                $token = '';
                $loginType = '未知';
                $where = [];
        }

        $isLogin = 0;
        // 查询账号信息
        $users = UsersModel::getUserInfo($where);
        if (!empty($users) && $users['status'] == 1) {
            Log::write('' . $loginType . '用户' . $users['nickname'] . '查询帖子详情:' . $token, 'error');
            $isLogin = 1;
        }

        $info['nickname'] = emojiDecode($info['nickname']);
        $path = PostPath::where('post_id', $info['id'])->hidden(['create_time'])->select();
        $path = $path ? $path->toArray() : [];
        $imagesPath = [];
        $videoPath = [];
        foreach ($path as $k1 => &$v1) {
//                var_dump($v1['path']);
            if ($v1['path']) {
                $v1['path'] = config('url.url') . $v1['path'];
                if ($v1['type'] == 2) {
                    $videoPath[] = $v1;
                } else {
                    $imagesPath[] = $v1;
                }
            }
        }

        $info['images'] = $imagesPath;
        $info['video'] = $videoPath;
        $info['times'] = friendlyDate(strtotime($info['create_time']));
        // 登录了，标识是否点赞了帖子
        $info['like_yourself'] = 0;
        if ($isLogin == 1) {
            $info['like_yourself'] = PostLike::where(['post_id' => $info['id'], 'uid' => $users['id']])->count();
        }
        $info['like'] = format_num(PostLike::where('post_id', $info['id'])->count());
        $info['share'] = format_num(intval(PostClickShare::where(['post_id' => $info['id'], 'type' => 2])->sum('nums')));
        $info['comment'] = PostComment::where('post_id', $info['id'])->count();

        $this->responseJson(10200, 'success', $info);
    }

    /**
     * 添加评论
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addPostComment()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '帖子添加评论操作', 'error');

        $param = $this->request->param();
        Log::write('评论数据:' . json_encode($param), 'error');

        if (!isset($param['uid']) || !isset($param['post_id']) || !isset($param['content'])) {
            $this->responseJson(10500, '参数错误');
        }

        if ($param['uid'] != $userInfo['id']) {
            $this->responseJson(10500, '不允许的操作');
        }

        if (empty($param['content'])) {
            $this->responseJson(10500, '内容不能为空');
        }

        if (isset($param['parent_id']) && $param['parent_id'] > 0) {
            // 一级评论下回复，需传递一级评论的id作为回复id
            if (!isset($param['reply_id']) && $param['reply_id'] <= 0) {
                $this->responseJson(10500, '参数1错误');
            }
        }

        if (isset($param['reply_id']) && $param['reply_id'] > 0) {
            // 二级评论下回复，需要传递二级评论的主键id
            if (!isset($param['parent_id']) && $param['parent_id'] <= 0) {
                $this->responseJson(10500, '参数2错误');
            }
            // 回复时间
            $param['reply_time'] = time();
        }

        $find = PostModel::where(['id' => $param['post_id']])->find();
        if (empty($find)) {
            $this->responseJson(10400, '帖子不存在');
        }

        // 本地测试不请求微信检测
        if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
            $wechat = new Wechat();
            $wxToken = $wechat->GetToken1();
            if (!$wxToken) {
                Log::write('addPostComment_token:' . json_encode($wxToken), 'error');
                $this->responseJson(10500, '验证标识出错');
            }

            $postData = json_encode(['content' => $param['content']], JSON_UNESCAPED_UNICODE);
            // 内容安全接口 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html
            // 频率限制：单个 appId 调用上限为 4000 次/分钟，2,000,000 次/天
            $url1 = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=%s";
            $checkUrl = sprintf($url1, $wxToken);
            Log::write('我是请求微信的token:' . $wxToken, 'error');
            $contentRes = httpCurl($checkUrl, $postData, 'POST');
            $contentRes = json_decode($contentRes, true);
            // 87014 => 内容含有违法违规内容
            if ($contentRes['errcode'] == 87014) {
                // 存储违规词
                $finds = SensitiveWord::where('badword', $param['content'])->find();
                if (empty($finds)) {
                    SensitiveWord::create([
                        'badword' => $param['content'],
                    ]);
                }
                Log::write('wx_content_errmsg：' . json_encode($contentRes), 'error');
                $this->responseJson(10500, '存在违规词汇1');
            }
        }

        // 获取敏感词汇
        $badWord = SensitiveWord::getBadWord();
        $result = SensitiveWord::checkBadWord($param['content'], $badWord);
        if ($result) {
            $this->responseJson(10500, '存在违规词汇2');
        }
//        var_dump($result);
//        exit;
        // 微信小程序个别不支持softbank编码，需转换成unified
        $param['content'] = \emoji\emoji_softbank_to_unified($param['content']);
        $res = PostComment::create($param);
        if (!$res) {
            $this->responseJson(10500, 'error');
        }

        $res = $res->toArray();
        $users = UsersModel::where('id', $res['uid'])->field('nickname,headimg')->find();
        $res['nickname'] = emojiDecode($users['nickname']);
        $res['reply_name'] = '';
        $res['create_time'] = friendlyDate(strtotime($res['create_time']));
        // 回复信息
//        if ($res['parent_id'] != 0) {
//            $findReply = PostComment::where(['id' => $res['parent_id']])->find();
//            // 谁回复了
//            if ($res['uid'] == $findReply['uid']) {
//                $res['nickname'] = '楼主';
//            }
//            // 回复了谁
//            if ($findReply['parent_id'] == 0) {
//                $res['reply_name'] = '楼主';
//            }
//        }

        // 属于回复，检测是否是楼主回复
        // 楼主判定：当前用户id等于帖子所属人id
        if ($res['reply_id'] != 0) {
            // 谁回复了
            $uid = PostModel::where('id', $res['post_id'])->value('uid');
            if ($res['uid'] == $uid) {
                $res['reply_name'] = '楼主';
            }
            // 回复了谁
            $reply_uid = PostComment::where(['id' => $res['reply_id']])->value('uid');
            if ($reply_uid == $uid) {
                $res['nickname'] = '楼主';
            }
        }
        $res['headimg'] = $users['headimg'];
        // 初始化二级评论
        $res['children'] = [];
        $res['count'] = $res['like'] = $res['like_yourself'] = 0;
        unset($res['reply_time'], $res['update_time']);
        $res['content'] = \emoji\emoji_softbank_to_unified($res['content']);
        $this->responseJson(10200, 'success', $res);
    }

    /**
     * 删除帖子评论
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delPostComment()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '删除帖子评论操作', 'error');

        $param = $this->request->param();
        Log::write('删除评论数据:' . json_encode($param), 'error');

        if (!isset($param['id']) || !isset($param['uid']) || !isset($param['post_id'])) {
            $this->responseJson(10500, '参数错误');
        }

        if ($param['uid'] != $userInfo['id']) {
            $this->responseJson(10500, '不允许的操作');
        }

        $where = ['id' => $param['id'], 'uid' => $param['uid'], 'post_id' => $param['post_id']];
        $find = PostComment::where($where)->find();
        if (empty($find)) {
            $this->responseJson(10500, '该评论不存在');
        }

        $res = PostComment::update(['status' => -1], $where);
        if (!$res) {
            $this->responseJson(10500, 'error');
        }

        $this->responseJson(10200, 'success');
    }

    /**
     * 点赞我的
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function postLikeMe()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '查看点赞我的帖子操作', 'error');

        $param = $this->request->param();
        if (!isset($param['uid'])) {
            $this->responseJson(10500, '参数错误');
        }

        if ($param['uid'] != $userInfo['id']) {
            $this->responseJson(10500, '不允许的操作');
        }

        $page = intval($this->request->param('page')) ?: 1;
        $pageSize = intval($this->request->param('pagesize')) ?: 10;

        // 点赞我的
        $idArr = PostModel::where('uid', $param['uid'])->whereIn('status', [0, 1, 4])->column('id');
        $data = PostLike::alias('pl')
            ->join('admin_users u', 'pl.uid = u.id', 'left')
            ->whereIn('pl.post_id', $idArr)
            ->page($page, $pageSize)
            ->order('pl.create_time', 'desc')
            ->field('pl.id,pl.uid,pl.post_id,pl.create_time,u.openid,u.nickname,u.headimg')
            ->select();
        $data = $data ? $data->toArray() : [];
        foreach ($data as $k => $v) {
            $data[$k]['times'] = friendlyDate(strtotime($v['create_time']));
            $data[$k]['nickname'] = emojiDecode($v['nickname']);
            $path = PostPath::where(['post_id' => $v['post_id'], 'type' => 1])->value('path');
            $data[$k]['images'] = $path ? config('url.url') . $path : '';
        }

        if (!$data) {
            $this->responseJson(10400, 'empty', ['data' => [], 'count' => 0]);
        }

        $count = PostLike::alias('pl')->whereIn('pl.post_id', $idArr)->count();

        $this->responseJson(10200, 'success', ['data' => $data, 'count' => $count]);

    }

    /**
     * 我点赞的
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function postMeLike()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '查看我点赞的帖子操作', 'error');

        $param = $this->request->param();
        if (!isset($param['uid'])) {
            $this->responseJson(10500, '参数错误');
        }

        if ($param['uid'] != $userInfo['id']) {
            $this->responseJson(10500, '不允许的操作');
        }

        $page = intval($this->request->param('page')) ?: 1;
        $pageSize = intval($this->request->param('pagesize')) ?: 10;

        $data = PostLike::alias('pl')
            ->join('admin_post p', 'pl.post_id = p.id', 'left')
            ->whereIn('p.status', [0, 1, 4])
            ->where('pl.uid', $param['uid'])
            ->field('pl.id,pl.uid,pl.post_id,pl.create_time,p.id as pid,p.content')
            ->page($page, $pageSize)
            ->select();
        $data = $data ? $data->toArray() : [];
        foreach ($data as $k => $v) {
            $data[$k]['times'] = friendlyDate(strtotime($v['create_time']));
            $data[$k]['images'] = config('url.url') . PostPath::where(['post_id' => $v['pid'], 'type' => 1])->value('path');
        }
//        var_dump($data);
//        exit;

        if (!$data) {
            $this->responseJson(10400, 'empty', ['data' => [], 'count' => 0]);
        }

        $count = PostLike::alias('pl')->join('admin_post p', 'pl.post_id = p.id', 'left')->whereIn('p.status', [0, 1, 4])->where('pl.uid', $param['uid'])->count();

        $this->responseJson(10200, 'success', ['data' => $data, 'count' => $count]);

    }

    /**
     * 分享我的
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function postShareMe()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '查看分享我的帖子操作', 'error');

        $param = $this->request->param();
        if (!isset($param['uid'])) {
            $this->responseJson(10500, '参数错误');
        }

        if ($param['uid'] != $userInfo['id']) {
            $this->responseJson(10500, '不允许的操作');
        }

        $page = intval($this->request->param('page')) ?: 1;
        $pageSize = intval($this->request->param('pagesize')) ?: 10;

        $idArr = PostModel::where('uid', $param['uid'])->whereIn('status', [0, 1, 4])->column('id');
        $data = PostClickShare::alias('s')
            ->join('admin_users u', 's.uid = u.id', 'left')
            ->whereIn('s.post_id', $idArr)
            // 分享类型 2
            ->where('s.type', 2)
            ->page($page, $pageSize)
            ->field('s.id,s.uid,s.share_type,s.post_id,s.create_time,u.id as uuid,u.nickname,u.headimg')
            ->select();
        $data = $data ? $data->toArray() : [];
        foreach ($data as $k => $v) {
            $data[$k]['share_type'] = $v['share_type'] == 1 ? '微信好友' : '微信朋友圈';
            $data[$k]['times'] = friendlyDate(strtotime($v['create_time']));
            $data[$k]['nickname'] = emojiDecode($v['nickname']);
            $data[$k]['images'] = config('url.url') . PostPath::where(['post_id' => $v['post_id'], 'type' => 1])->value('path');
        }

        if (!$data) {
            $this->responseJson(10400, 'empty', ['data' => [], 'count' => 0]);
        }

        $count = PostClickShare::alias('s')->whereIn('s.post_id', $idArr)->where('s.type', 2)->count();

        $this->responseJson(10200, 'success', ['data' => $data, 'count' => $count]);

    }

    /**
     * 我分享的
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function postMeShare()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '查看我分享的帖子操作', 'error');

        $param = $this->request->param();
        if (!isset($param['uid'])) {
            $this->responseJson(10500, '参数错误');
        }

        if ($param['uid'] != $userInfo['id']) {
            $this->responseJson(10500, '不允许的操作');
        }

        $page = intval($this->request->param('page')) ?: 1;
        $pageSize = intval($this->request->param('pagesize')) ?: 10;

        $data = PostClickShare::alias('s')
            ->join('admin_post p', 's.post_id = p.id', 'left')
            ->whereIn('p.status', [0, 1, 4])
            ->where('s.uid', $param['uid'])
            ->field('s.id,s.uid,s.post_id,s.share_type,s.create_time,p.id as pid,p.content')
            ->page($page, $pageSize)
            ->select();
        $data = $data ? $data->toArray() : [];
        foreach ($data as $k => $v) {
            $data[$k]['share_type'] = $v['share_type'] == 1 ? '微信好友' : '微信朋友圈';
            $data[$k]['times'] = friendlyDate(strtotime($v['create_time']));
            $data[$k]['images'] = config('url.url') . PostPath::where(['post_id' => $v['pid'], 'type' => 1])->value('path');
        }
//        var_dump($data);
//        exit;

        if (!$data) {
            $this->responseJson(10400, 'empty', ['data' => [], 'count' => 0]);
        }

        $count = PostClickShare::alias('s')->join('admin_post p', 's.post_id = p.id', 'left')->whereIn('p.status', [0, 1, 4])->where('s.uid', $param['uid'])->count();

        $this->responseJson(10200, 'success', ['data' => $data, 'count' => $count]);

    }

    /**
     * 评论列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function postCommentList()
    {
        $param = $this->request->param();
        if (!isset($param['id'])) {
            $this->responseJson(10500, '参数错误');
        }

        $page = intval($this->request->param('page')) ?: 1;
        $pageSize = intval($this->request->param('pagesize')) ?: 10;

        $topComment = PostComment::getCommentList($param['id'], 0, 'desc', $page, $pageSize);
        if (!$topComment) {
            $this->responseJson(10400, 'empty', ['data' => [], 'count' => 0]);
        }

        $type = $this->request->header('type');
        switch ($type) {
            case 0:
                $token = $this->request->header('token');
                $loginType = '小程序';
                $where = ['token' => $token];
                break;
            case 1:
                $token = $this->request->header('token1');
                $loginType = '公众号';
                $where = ['token1' => $token];
                break;
            default:
                $token = '';
                $loginType = '未知';
                $where = [];
        }

        $isLogin = 0;
        // 查询账号信息
        $loginUsers = UsersModel::getUserInfo($where);
        if (!empty($loginUsers) && $loginUsers['status'] == 1) {
            Log::write('' . $loginType . '用户' . $loginUsers['nickname'] . '查询评论列表:' . $token, 'error');
            $isLogin = 1;
        }

        foreach ($topComment as $key => $val) {
            // 标识是否是楼主
            $topUid = PostModel::where('id', $val['post_id'])->value('uid');
            $topComment[$key]['is_oriPoster'] = $val['uid'] == $topUid ? 1 : 0;
            $topComment[$key]['like'] = format_num(PostCommentLike::where(['comment_id' => $val['id']])->count());
            // 获取用户信息
            $users = UsersModel::where('id', $val['uid'])->find();
            $topComment[$key]['nickname'] = emojiDecode($users['nickname']);
            $topComment[$key]['headimg'] = $users['headimg'];
            $topComment[$key]['create_time'] = friendlyDate(strtotime($val['create_time']));
//            $topComment[$key]['create_time'] = date('n'."月".'d'."日".' H:i',strtotime($val['create_time']));
            // 登录了，标识是否点赞了帖子
            $topComment[$key]['like_yourself'] = 0;
            if ($isLogin == 1) {
                $topComment[$key]['like_yourself'] = PostCommentLike::where(['comment_id' => $val['id'], 'uid' => $loginUsers['id']])->count();
            }
            $topComment[$key]['content'] = \emoji\emoji_softbank_to_unified($val['content']);

            // 获取所有二级评论
            $childComment = PostComment::getCommentList($param['id'], $val['id'], 'asc', 0);
            $topComment[$key]['children'] = [];
            foreach ($childComment as $value) {
//                var_dump($value['uid'],$val['uid']);
                $value['content'] = \emoji\emoji_softbank_to_unified($value['content']);

                $value['create_time'] = friendlyDate(strtotime($value['create_time']));
//                if ($value['uid'] == $val['uid']) {
//                    $value['nickname'] = '楼主';
//                } else {
//                    $nickname = UsersModel::where('id', $value['uid'])->value('nickname');
//                    $value['nickname'] = emojiDecode($nickname);
//                }
                $value['reply_name'] = '';
                $nickname = UsersModel::where('id', $value['uid'])->value('nickname');
                $value['nickname'] = emojiDecode($nickname);
                // 根据回复id获取回复人对应的openid
                if ($value['reply_id'] != 0) {
                    // 查询帖子所属人
                    $uid = PostModel::where('id', $value['post_id'])->value('uid');
                    $reply_uid = PostComment::where(['id' => $value['reply_id']])->value('uid');
                    // 谁回复了
                    if ($value['uid'] == $uid) {
                        $value['reply_name'] = '楼主';
                    } else {
//                        var_dump($reply_uid);
                        $reply_name = UsersModel::where('id', $reply_uid)->value('nickname');
                        $value['reply_name'] = emojiDecode($reply_name);
                    }
                    // 回复了谁
//                    var_dump($reply_uid,$uid);
//                    exit;
                    if ($reply_uid == $uid) {
                        $value['nickname'] = '楼主';
                    } else {
//                        // 获取用户信息
                        $nickname = UsersModel::where('id', $value['uid'])->value('nickname');
                        $value['nickname'] = emojiDecode($nickname);
                    }

//                    $value['reply_uid'] = PostComment::where(['id' => $value['reply_id']])->value('uid');
//                    if ($value['reply_uid'] == $val['uid']) {
//                        $value['reply_name'] = '楼主';
//                    } else {
//                        // 获取用户信息
//                        $reply_name = UsersModel::where('id', $value['reply_uid'])->value('nickname');
//                        $value['reply_name'] = $reply_name ? emojiDecode($reply_name) : '';
//                    }
                }
                $topComment[$key]['children'][] = $value;
            }
        }

        $count = PostComment::getCommentCount($param['id'], 0);

        $this->responseJson(10200, 'success', ['data' => $topComment, 'count' => $count]);
    }

    /**
     * 评论点赞操作
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function postCommentLike()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '帖子点赞操作', 'error');

        if (!$this->request->isPost()) {
            $this->responseJson(10500, '发生异常');
        }

        $param = $this->request->param();
        if (!isset($param['uid']) || !isset($param['id']) || !isset($param['operate'])) {
            $this->responseJson(10500, '参数错误');
        }

        if ($param['uid'] != $userInfo['id']) {
            $this->responseJson(10500, '不允许的操作');
        }

        $isExists = PostComment::where(['parent_id' => 0, 'id' => $param['id']])->find();
        if (empty($isExists)) {
            $this->responseJson(10400, 'empty');
        }

        $where = ['uid' => $param['uid'], 'comment_id' => $param['id']];
        // 是否已点赞
        $find = PostCommentLike::where($where)->find();
        // 点赞
        if ($param['operate'] >= 1) {
            // 不存在，写入
            if (empty($find)) {
                $res = PostCommentLike::create($where);
            }
            // 取消点赞
        } else {
            // 存在，删除
            if (!empty($find)) {
                $res = PostCommentLike::where($where)->delete();
            }
        }

        $this->responseJson(10200, 'success', ['id' => $param['id'], 'nums' => $param['operate']]);
    }

    /**
     * 帖子搜索
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function postSearch()
    {
        $param = $this->request->param();
        if (!isset($param['keywords'])) {
            $this->responseJson(10500, '参数错误');
        }

        if (empty($param['keywords'])) {
            $this->responseJson(10400, 'empty', ['data' => [], 'count' => 0]);
        }

        $page = intval($this->request->param('page')) ?: 1;
        $pageSize = intval($this->request->param('pagesize')) ?: 10;

        // 1正常   4已通过（前端提交的帖子）
        $status = '1,4';
        $whereOr = [];
        $type = $this->request->header('type');
        switch ($type) {
            case 0:
                $token = $this->request->header('token');
                $loginType = '小程序';
                $where = ['token' => $token];
                break;
            case 1:
                $token = $this->request->header('token1');
                $loginType = '公众号';
                $where = ['token1' => $token];
                break;
            default:
                $token = '';
                $loginType = '未知';
                $where = [];
        }

        $isLogin = 0;
        // 查询账号信息
        $users = UsersModel::getUserInfo($where);
        if (!empty($users) && $users['status'] == 1) {
            Log::write('' . $loginType . '用户' . $users['nickname'] . '查询待审核帖子:' . $token, 'error');
            $isLogin = 1;
            // 用户已登录，根据uid查询是否存在待审核帖子
            $loginWhere = ['uid' => $users['id'], 'status' => 0];
            $idArr = PostModel::where($loginWhere)->column('id');
//                var_dump($idArr);
        }

        if (isset($idArr) && !empty($idArr)) {
            $idStr = rtrim(join(',', $idArr), ',');
            $whereOr[] = ['p.id', 'in', $idStr];
        }

        // 搜索条件
        $like[] = ['p.content|u.nickname', 'like', '%' . $param['keywords'] . '%'];

        $info = PostModel::alias('p')
            ->join('admin_users u', 'p.uid = u.id', 'left')
            ->join('admin_post_cate c', 'p.pcid = c.id', 'left')
            ->field('p.id,p.rid,p.status,p.uid,p.pcid,p.content,p.location,p.create_time,u.nickname,u.headimg,c.name cate_name,p.lng,p.lat')
            ->where($like)
            ->whereOr($whereOr)
            ->whereIn('p.status', $status)
            ->order('p.create_time', 'desc')
            ->page($page, $pageSize)
            ->select();
//        var_dump($info);exit;

        $info = $info ? $info->toArray() : [];
        if (!$info) {
            $this->responseJson(10400, 'empty', ['data' => [], 'count' => 0]);
        }

        $count = PostModel::alias('p')->join('admin_users u', 'p.uid = u.id', 'left')->where($like)->whereOr($whereOr)->whereIn('p.status', $status)->count();

        foreach ($info as $k => $v) {
            // 获取社群数据
            if ($v['rid']) {
                $resource = iResource::alias('r')
                    ->join('admin_location p', 'r.province = p.location_id', 'left')
                    ->join('admin_location c', 'r.city = c.location_id', 'left')
                    ->join('admin_users u', 'r.uid = u.id', 'left')
                    ->where(['r.id' => $v['rid']])
                    ->field('r.id,r.name,r.qrcode,r.desc,r.content,u.headimg,p.name pname,c.name cname')
                    ->find();
                if (!empty($resource)) {
                    $resource['qrcode'] = config('url.url') . $resource['qrcode'];
                    if ($resource['content']) {
                        $content = cutstr_html($resource['content']);
                        $content = mb_substr($content, 0, 150, 'UTF-8');
                        $resource['content'] = $content;
                    }
                    $resource['collect_yourself'] = 0;
                    if ($isLogin) {
                        $isCollect = ResourceCollect::where(['uid' => $users['id'], 'rid' => $v['rid']])->find();
                        // 收藏了
                        if (!empty($isCollect)) {
                            $resource['collect_yourself'] = 1;
                        }
                    }

                    $resource['location'] = str_replace(['省', '市'], '', $resource['pname'] . '·' . $resource['cname']);
                    unset($resource['pname'], $resource['cname']);

                    $images = ResourcePath::where(['rid' => $resource['id'], 'type' => 1])->field('id,path')->select();
                    $images = $images ? $images->toArray() : [];
                    foreach ($images as $k1 => $v1) {
                        $images[$k1]['path'] = config('url.url') . $v1['path'];
                    }
                    $resource['images'] = $images;
                }
            } else {
                $resource = [];
            }

            $info[$k]['resource'] = $resource;
            $info[$k]['nickname'] = emojiDecode($v['nickname']);
            $path = PostPath::where('post_id', $v['id'])->hidden(['create_time'])->select();
            $path = $path ? $path->toArray() : [];

            $imagesPath = [];
            $videoPath = [];
            foreach ($path as $k1 => &$v1) {
//                var_dump($v1['path']);
                if ($v1['path']) {
                    $v1['path'] = config('url.url') . $v1['path'];
                    if ($v1['type'] == 2) {
                        $videoPath[] = $v1;
                    } else {
                        $imagesPath[] = $v1;
                    }
                }
            }

            if (count($imagesPath) <= 1) {
                foreach ($imagesPath as $k2 => $v2) {
                    $tmp = [$v2['width'], $v2['height']];
                    $max = max($tmp);
                    $min = min($tmp);
                    $lastSize = $max - $min;
                    $imagesPath[$k2]['dis_method'] = 'aspectFill';
                    $imagesPath[$k2]['heights'] = '300px';
                    if ($lastSize >= 200) {
                        $keys = array_search($max, $tmp);
                        // 1高度  2宽度
                        $imagesPath[$k2]['dis_method'] = $keys == 1 ? 'heightFix' : 'widthFix';
                        $imagesPath[$k2]['heights'] = $keys == 1 ? '250px' : 'auto';
                    }
                }
            }

            $info[$k]['images'] = $imagesPath;
            $info[$k]['video'] = $videoPath;

            $info[$k]['times'] = friendlyDate(strtotime($v['create_time']));
            // 登录了，标识是否点赞了帖子
            $info[$k]['like_yourself'] = 0;
            if ($isLogin == 1) {
                $info[$k]['like_yourself'] = PostLike::where(['post_id' => $v['id'], 'uid' => $users['id']])->count();
            }
            $info[$k]['like'] = format_num(PostLike::where('post_id', $v['id'])->count());
            $info[$k]['share'] = format_num(intval(PostClickShare::where(['post_id' => $v['id'], 'type' => 2])->sum('nums')));
            $info[$k]['comment'] = PostComment::where('post_id', $v['id'])->count();
        }

        $this->responseJson(10200, 'success', ['data' => $info, 'count' => $count]);
    }

    /**
     * 删除帖子
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delPost()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '帖子删除操作', 'error');

        if (!$this->request->isPost()) {
            $this->responseJson(10500, '发生异常');
        }

        $param = $this->request->param();
        if (!isset($param['uid']) || !isset($param['id'])) {
            $this->responseJson(10500, '参数错误');
        }

        if ($param['uid'] != $userInfo['id']) {
            $this->responseJson(10500, '不允许的操作');
        }

        $find = PostModel::where('id', $param['id'])->find();
        if (empty($find)) {
            $this->responseJson(10500, '数据不存在');
        }

        if ($userInfo['id'] != $find['uid']) {
            $this->responseJson(10500, '无执行权限');
        }

        $res = PostModel::update(['status' => -1], ['id' => $param['id']]);
        if (!$res) {
            $this->responseJson(10500, 'error');
        }

        $this->responseJson(10200, 'success', ['id' => $param['id']]);
    }


}